import image_utils
import json
import requests
import logging

def get_liveness_score(image_path, provider):
    #
    """调用活体检测API获取活体分数"""
    try:
        image_base64 = image_utils.read_image_as_base64(image_path)

        url = "http://jdzy.dev.esandcloud.com/facial_recognition/liveness"
        payload = json.dumps({
            "enableLiveness": True,
            "livenessProvider": provider,
            "image": image_base64
        })
        headers = {
            'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", url, headers=headers, data=payload)
        return float(response.text) if response.text else None
    except Exception as e:
        logging.error(f"处理图片 {image_path} 时发生错误: {str(e)}")
        return None
